package com.yunxi.service.dubboService.impl.sell;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.baseDataToHub.pojo.app.VehiclePackageConfigVO;
import com.yunxi.baseDataToHub.pojo.app.VehiclePackagePriceVO;
import com.yunxi.baseDataToHub.pojo.app.VehicleRetailPriceVO;
import com.yunxi.baseDataToHub.service.app.AppDubboService;
import com.yunxi.core.util.SortUtil;
import com.yunxi.core.util.WgrDate;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.dealer.service.PartnerDubboService;
import com.yunxi.model.sell.CarSellPartner;
import com.yunxi.sell.pojo.*;
import com.yunxi.sell.service.CarSellPartnerDubboService;
import com.yunxi.service.localService.CarSellPartnerService;
import com.yunxi.sys.pojo.AreaVO;
import com.yunxi.sys.service.AreaDubboService;
import com.yunxi.vehicle.pojo.*;
import com.yunxi.vehicle.service.VehicleConfigDubboService;
import com.yunxi.vehicle.service.VehiclePackageDubboService;
import com.yunxi.vehicle.service.VehicleSpectrumDubboService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车辆销售渠道管理 服务实现类
 * </p>
 * @author
 * @since 2019-08-21
 */
public class CarSellPartnerDubboServiceImpl implements CarSellPartnerDubboService {

    private static final Logger logger = LoggerFactory.getLogger(CarSellPartnerDubboServiceImpl.class);

    @Autowired
    @Qualifier(value = "carSellPartnerServiceImpl")
    CarSellPartnerService carSellPartnerService;

    @Autowired
    VehiclePackageDubboService vehiclePackageDubboService;

    @Autowired
    VehicleSpectrumDubboService vehicleSpectrumDubboService;

    @Autowired
    VehicleConfigDubboService vehicleConfigDubboService;

    @Autowired
    private AppDubboService appDubboServiceImpl;

    @Autowired
    private PartnerDubboService partnerDubboServiceImpl;

    @Autowired
    private AreaDubboService areaDubboService;

    @Override
    public CarSellPartnerQueryRespVO getById(String id) throws InvocationTargetException, IllegalAccessException {
    CarSellPartner carSellPartner = carSellPartnerService.getById(id);
        CarSellPartnerQueryRespVO carSellPartnerVO = new CarSellPartnerQueryRespVO();
        BeanUtils.copyProperties(carSellPartner,carSellPartnerVO);
        return carSellPartnerVO;
    }

    @Override
    @Transactional
    public ApiResult save(CarSellPartnerSaveReqVO carSellPartnerVO)  {
        String sellId = carSellPartnerVO.getSellId();
        if(StringUtils.isNotEmpty(sellId)){
            //修改
            carSellPartnerVO.setOptionType(2);
        }else{
            //新增
            carSellPartnerVO.setOptionType(1);
        }

        List<String> retMsg = new ArrayList<>();

//        CarSellPartnerVerifyExistsReqVO existsReqVO = new CarSellPartnerVerifyExistsReqVO();
//        existsReqVO.setCatalogCode(carSellPartnerVO.getCatalogCode());
        //existsReqVO.setCustomPacks(carSellPartnerVO.getCustomPacks());
//        if(verifyExists(existsReqVO)){
//            logger.info("配置+选装包已存在,请核实！");
//            retMsg.add("配置+选装包已存在,请核实！");
//            return ResponseUtil.fail(901,"操作提示",retMsg);
//        }

        //
        //发送BOM选装包数据给APP
        List<VehiclePackageForAppRespVO> vehiclePackageForAppList = new ArrayList<>();

        //得到已配置的选装包分类
        String[] customPacks = carSellPartnerVO.getCustomPacks().split(",");
        for(String item : customPacks){
            VehiclePackageForAppReqVO vehiclePackageForAppReqVO = new VehiclePackageForAppReqVO();
            vehiclePackageForAppReqVO.setNodeCode(carSellPartnerVO.getCatalogCode());
            vehiclePackageForAppReqVO.setCustomPack(item);
            //根据车型及选装包分类 查询 选装包数据
            List<VehiclePackageForAppRespVO> vehiclePackageForApp_Tmp = vehiclePackageDubboService.queryVehiclePackageByApp(vehiclePackageForAppReqVO);
            if(vehiclePackageForApp_Tmp.size()>0){
                //如果查询的价格为空 则查询最新的
                if(vehiclePackageForApp_Tmp.get(0).getAmount()!=null
                        &&(vehiclePackageForApp_Tmp.get(0).getAmount().intValue()==0
                        ||vehiclePackageForApp_Tmp.get(0).getAmount().intValue()<0)){
                    //vehiclePackageForApp_Tmp = vehiclePackageDubboService.queryVehiclePackageByAppLast(vehiclePackageForAppReqVO);
                }else{
                    vehiclePackageForAppList.addAll(vehiclePackageForApp_Tmp);
                }
            }
        }

        //
        //发送BOM基础车型数据给APP
        VehicleSpectrumReqVO vehicleSpectrumReqVO = new VehicleSpectrumReqVO();
        vehicleSpectrumReqVO.setNodeCode(carSellPartnerVO.getCatalogCode());

        List<VehicleSpectrumRespVO> vehicleSpectrumForApp = vehicleSpectrumDubboService.queryVehicleSpectrumByApp(vehicleSpectrumReqVO);

        if(vehicleSpectrumForApp.size()>0){
            if(vehicleSpectrumForApp.get(0)!=null&&vehicleSpectrumForApp.get(0).getAmount()!=null) {
                if (vehicleSpectrumForApp.get(0).getAmount().equals("0")
                        || vehicleSpectrumForApp.get(0).getAmount().indexOf("-") != -1) {
                    vehicleSpectrumForApp = vehicleSpectrumDubboService.queryVehicleSpectrumByAppLast(vehicleSpectrumReqVO);
                }
            }
        }

        VehicleRetailPriceVO vehicleRetailPriceVO = new VehicleRetailPriceVO();
        if(vehicleSpectrumForApp != null && vehicleSpectrumForApp.size() > 0) {

            vehicleRetailPriceVO.setExt1(carSellPartnerVO.getAreas());
            vehicleRetailPriceVO.setRegionCode(carSellPartnerVO.getAreas());

            if(StringUtils.isEmpty(carSellPartnerVO.getAreas())){
                 //
                //取出所有的区域和渠道商
                List<PartnerVO> partnerVOs = partnerDubboServiceImpl.listByVo(new PartnerVO());
                String partnerCode = "";
                for(int i = 0; i < partnerVOs.size(); i ++){
                    if(i == 0){
                        partnerCode = partnerVOs.get(i).getPartnerCode();
                    }else{
                        partnerCode += "," + partnerVOs.get(i).getPartnerCode();
                    }
                }
                vehicleRetailPriceVO.setPartnerCode(partnerCode);

                AreaVO areaVo = new AreaVO();
                areaVo.setType(0);
                areaVo.setPcode("0");
                List<AreaVO> areaVOs =  (List<AreaVO>) areaDubboService.listByVo(areaVo,null);

                String areas = "";
                for(int i = 0; i < areaVOs.size(); i ++){
                    if(i == 0){
                        areas = areaVOs.get(i).getCode();
                    }else{
                        areas += "," + areaVOs.get(i).getCode();
                    }
                }
                vehicleRetailPriceVO.setRegionCode(areas);
                vehicleRetailPriceVO.setExt1(areas);
            }else{
                if(StringUtils.isEmpty(carSellPartnerVO.getPartnerCodes())){
                    //
                    // 取出当前区域的所有渠道商
                    String[] areaArray = carSellPartnerVO.getAreas().split(",");
                    List<PartnerVO> partnerVOs_tmp = new ArrayList<>();
                    for(String item : areaArray){
                        PartnerVO partnerVO = new PartnerVO();
                        partnerVO.setProvince(item);
                        List<PartnerVO> partnerVOs = partnerDubboServiceImpl.listByVo(partnerVO);
                        partnerVOs_tmp.addAll(partnerVOs);
                    }
                    String partnerCodes = "";
                    for(int i = 0; i < partnerVOs_tmp.size(); i ++){
                        if(i == 0){
                            partnerCodes = partnerVOs_tmp.get(i).getPartnerCode();
                        }else{
                            partnerCodes += "," + partnerVOs_tmp.get(i).getPartnerCode();
                        }
                    }
                    vehicleRetailPriceVO.setPartnerCode(partnerCodes);
                }
            }

            vehicleRetailPriceVO.setNodeCode(vehicleSpectrumForApp.get(0).getNodeCode());
            vehicleRetailPriceVO.setNodeName(vehicleSpectrumForApp.get(0).getNodeName());
            vehicleRetailPriceVO.setNodeType(vehicleSpectrumForApp.get(0).getNodeType());
            vehicleRetailPriceVO.setOptionType(carSellPartnerVO.getOptionType());
            vehicleRetailPriceVO.setParentNodeCode(vehicleSpectrumForApp.get(0).getParentNodeCode());
            vehicleRetailPriceVO.setParentNodeName(vehicleSpectrumForApp.get(0).getParentNodeName());
            vehicleRetailPriceVO.setProductTypeCode(vehicleSpectrumForApp.get(0).getProductTypeCode());
            vehicleRetailPriceVO.setProductTypeName(vehicleSpectrumForApp.get(0).getProductTypeName());
            vehicleRetailPriceVO.setVehicleRetailPrice(vehicleSpectrumForApp.get(0).getAmount());
            vehicleRetailPriceVO.setVehicleTypeStatus(vehicleSpectrumForApp.get(0).getStatus() == null ? "1":vehicleSpectrumForApp.get(0).getStatus());

            appDubboServiceImpl.sendVehicleRetailPricePMS2APP(vehicleRetailPriceVO);
        }else{
             logger.info("【BOM基础车型】该"+carSellPartnerVO.getCatalogCode()+"车型没有数据，推送APP失败，请核实！");
             retMsg.add("【BOM基础车型】该"+carSellPartnerVO.getCatalogCode()+"车型没有数据，推送APP失败，请核实！");
        }

        List<VehiclePackagePriceVO> sendVehiclePackagePrice = new ArrayList<>();
        //发送选装包价格数据到APP
        for(VehiclePackageForAppRespVO vo : vehiclePackageForAppList){
            VehiclePackagePriceVO result = new VehiclePackagePriceVO();
            result.setFamilyCode(vo.getFamilyCode());
            result.setFamilyName(vo.getFamilyName());
            result.setFamilyNameEn(vo.getFamilyName());
            result.setFeatureCode(vo.getFeatureCode());
            result.setFeatureContent(vo.getFeatureContent());
            result.setFeatureDesc(vo.getFeatureDesc());
            result.setFeatureDescEn(vo.getFeatureDescEn());
            result.setFeatureEngName("");
            result.setFeatureName(vo.getFeatureName());
            result.setOptionType(carSellPartnerVO.getOptionType());
            result.setVehicleStatus(vo.getStatus() != null ? vo.getStatus().toString() : "1" );
            result.setPackageRetailPrice(vo.getAmount() != null ?vo.getAmount().toString():"0");
            result.setFeatureValueStatus(vo.getFeatureValueStatus());
            result.setVehicleCode(vo.getProductCode());
            sendVehiclePackagePrice.add(result);
        }

        if(sendVehiclePackagePrice.size() > 0) {
            appDubboServiceImpl.sendVehiclePackagePricePMS2APP(sendVehiclePackagePrice);
        }else{
            logger.info("【BOM选装包】查询没有有效数据，推送APP失败，请核实！");
            retMsg.add("【BOM选装包】查询没有有效数据，推送APP失败，请核实！");
        }

        //BOM内外饰配置
        List<VehicleConfigInOutColorQueryRespVO> vehicleConfigInOutColorForApp = new ArrayList<>();
        String[] inColors = carSellPartnerVO.getColorIdIns().split(",");
        for(String item : inColors){
            //BOM内饰配置
            VehicleConfigInOutColorQueryReqVO vehicleConfigInOutColorQueryReqVO = new VehicleConfigInOutColorQueryReqVO();
            vehicleConfigInOutColorQueryReqVO.setNodeCode(carSellPartnerVO.getCatalogCode());
            vehicleConfigInOutColorQueryReqVO.setColor(item);

            List<VehicleConfigInOutColorQueryRespVO> vehicleConfigInOutColorForApp_Tmp = vehicleConfigDubboService.queryVehicleConfigInColorByApp(vehicleConfigInOutColorQueryReqVO);
            vehicleConfigInOutColorForApp.addAll(vehicleConfigInOutColorForApp_Tmp);
        }

        String[] outColors = carSellPartnerVO.getColorIdOuts().split(",");
        for(String item : outColors){
            //BOM外饰配置
            VehicleConfigInOutColorQueryReqVO vehicleConfigInOutColorQueryReqVO = new VehicleConfigInOutColorQueryReqVO();
            vehicleConfigInOutColorQueryReqVO.setNodeCode(carSellPartnerVO.getCatalogCode());
            vehicleConfigInOutColorQueryReqVO.setColor(item);

            List<VehicleConfigInOutColorQueryRespVO> vehicleConfigInOutColorForApp_Tmp = vehicleConfigDubboService.queryVehicleConfigOutColorByApp(vehicleConfigInOutColorQueryReqVO);
            vehicleConfigInOutColorForApp.addAll(vehicleConfigInOutColorForApp_Tmp);
        }

        List<VehiclePackageConfigVO> vehiclePackageConfigVO = new ArrayList<>();

        for(VehicleConfigInOutColorQueryRespVO vo : vehicleConfigInOutColorForApp){

            VehiclePackageConfigVO entity = new VehiclePackageConfigVO();
            entity.setEffectiveFrom(WgrDate.date2Str(vo.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
            entity.setFamilyCode(vo.getFamilyCode());
            entity.setFamilyName(vo.getFamilyName());
            entity.setFeatureCode(vo.getFeatureCode());
            entity.setFeatureName(vo.getFeatureName());
            entity.setFeatureValueStatus(vo.getFeatureValueStatus());
            entity.setProductNodeCode(vo.getParentNodeCode());
            entity.setOptionType(carSellPartnerVO.getOptionType());
            entity.setStatus("1");
            entity.setVehicleCode(vo.getVehicleCode());
            entity.setNodeNme(vo.getNodeName());
            vehiclePackageConfigVO.add(entity);
        }

        if(vehiclePackageConfigVO.size() > 0){
            appDubboServiceImpl.sendVehiclePackagePMS2APP(vehiclePackageConfigVO);
        }else{
            logger.info("【BOM内外饰配置】查询没有有效数据，推送APP失败，请核实！");
            retMsg.add("【BOM内外饰配置】查询没有有效数据，推送APP失败，请核实！");
        }

        CarSellPartner carSellPartner = new CarSellPartner();
        String[] src = carSellPartnerVO.getCustomPacks().split(",");
        SortUtil.sort(src);
        BeanUtils.copyProperties(carSellPartnerVO,carSellPartner);
        carSellPartner.setCustomPacks(String.join(",",src));
        //carSellPartner.setAreas(vehicleRetailPriceVO.getExt1());
        //carSellPartner.setPartnerCodes(vehicleRetailPriceVO.getPartnerCode());
        carSellPartnerService.saveOrUpdate(carSellPartner);

        if(retMsg.size() > 0){
            throw new RuntimeException(JSONArray.toJSONString(retMsg));
        }else{
            return ResponseUtil.ok();
        }
    }

    @Override
    @Transactional
    public ApiResult saveCas(CarSellPartnerSaveReqVO carSellPartnerVO)  {
        StringBuffer stringBuffer = new StringBuffer();
        List<String> retMsg = new ArrayList<>();

        //会影响接口调用
        carSellPartnerVO.setOptionType(1);
        if(StringUtils.isNotEmpty(carSellPartnerVO.getSellId())){
            carSellPartnerVO.setOptionType(2);//修改
        }

        //1、如果整车型谱没有数据或没有价格，则抛出异常，无法推送app
        //2、如果所选的选装包中部分数据没有价格或者有价格但是价格为0，则抛出异常，无法推送app
        //3、如果内饰外饰型谱数据不存在，则无法推送app

        /**
         * 查询整车型谱记录
         */
        VehicleSpectrumReqVO vehicleSpectrumReqVO = new VehicleSpectrumReqVO();
        vehicleSpectrumReqVO.setNodeCode(carSellPartnerVO.getCatalogCode());
        List<VehicleSpectrumRespVO> vehicleSpectrumForApp = vehicleSpectrumDubboService.queryVehicleSpectrumByApp(vehicleSpectrumReqVO);

        VehicleRetailPriceVO vehicleRetailPriceVO = new VehicleRetailPriceVO();
        if(vehicleSpectrumForApp != null && vehicleSpectrumForApp.size() > 0) {
            VehicleSpectrumRespVO spectrum = vehicleSpectrumForApp.get(0);
             if(spectrum.getAmount()!=null
                     &&(spectrum.getAmount().equals("0")
                        || spectrum.getAmount().indexOf("-") != -1)) {
                 retMsg.add("【BOM基础车型】该"+carSellPartnerVO.getCatalogCode()+"车型维护价格为0，不能放行");
            }else if(StringUtils.isEmpty(spectrum.getAmount())){
                 retMsg.add("【BOM基础车型】该"+carSellPartnerVO.getCatalogCode()+"车型没有维护价格，不能放行");
            }
            else{
                vehicleRetailPriceVO.setExt1(carSellPartnerVO.getAreas());
                vehicleRetailPriceVO.setRegionCode(carSellPartnerVO.getAreas());

                if(StringUtils.isEmpty(carSellPartnerVO.getAreas())){
                    //取出所有的区域和渠道商
                    List<PartnerVO> partnerVOs = partnerDubboServiceImpl.listByVo(new PartnerVO());
                    String partnerCode = "";
                    for(int i = 0; i < partnerVOs.size(); i ++){
                        if(i == 0){
                            partnerCode = partnerVOs.get(i).getPartnerCode();
                        }else{
                            partnerCode += "," + partnerVOs.get(i).getPartnerCode();
                        }
                    }
                    vehicleRetailPriceVO.setPartnerCode(partnerCode);

                    AreaVO areaVo = new AreaVO();
                    areaVo.setType(0);
                    areaVo.setPcode("0");
                    List<AreaVO> areaVOs =  (List<AreaVO>) areaDubboService.listByVo(areaVo,null);

                    String areas = "";
                    for(int i = 0; i < areaVOs.size(); i ++){
                        if(i == 0){
                            areas = areaVOs.get(i).getCode();
                        }else{
                            areas += "," + areaVOs.get(i).getCode();
                        }
                    }
                    vehicleRetailPriceVO.setRegionCode(areas);
                    vehicleRetailPriceVO.setExt1(areas);
                }else{
                    if(StringUtils.isEmpty(carSellPartnerVO.getPartnerCodes())){
                        //
                        // 取出当前区域的所有渠道商
                        String[] areaArray = carSellPartnerVO.getAreas().split(",");
                        List<PartnerVO> partnerVOs_tmp = new ArrayList<>();
                        for(String item : areaArray){
                            PartnerVO partnerVO = new PartnerVO();
                            partnerVO.setProvince(item);
                            List<PartnerVO> partnerVOs = partnerDubboServiceImpl.listByVo(partnerVO);
                            partnerVOs_tmp.addAll(partnerVOs);
                        }
                        String partnerCodes = "";
                        for(int i = 0; i < partnerVOs_tmp.size(); i ++){
                            if(i == 0){
                                partnerCodes = partnerVOs_tmp.get(i).getPartnerCode();
                            }else{
                                partnerCodes += "," + partnerVOs_tmp.get(i).getPartnerCode();
                            }
                        }
                        vehicleRetailPriceVO.setPartnerCode(partnerCodes);
                    }
                }

                vehicleRetailPriceVO.setNodeCode(spectrum.getNodeCode());
                vehicleRetailPriceVO.setNodeName(spectrum.getNodeName());
                vehicleRetailPriceVO.setNodeType(spectrum.getNodeType());
                vehicleRetailPriceVO.setOptionType(carSellPartnerVO.getOptionType());
                vehicleRetailPriceVO.setParentNodeCode(spectrum.getParentNodeCode());
                vehicleRetailPriceVO.setParentNodeName(spectrum.getParentNodeName());
                vehicleRetailPriceVO.setProductTypeCode(spectrum.getProductTypeCode());
                vehicleRetailPriceVO.setProductTypeName(spectrum.getProductTypeName());
                vehicleRetailPriceVO.setVehicleRetailPrice(spectrum.getAmount());
                vehicleRetailPriceVO.setVehicleTypeStatus(spectrum.getStatus() == null ? "1":spectrum.getStatus());
            }
        }else{
            logger.info("【BOM基础车型】没有在型谱中查询到数据，无法推送APP，请核实！");
            retMsg.add("【BOM基础车型】没有在型谱中查询到数据，无法推送APP，请核实！");
        }

        /**
         * ****************************************************************** 查询BOM内外饰配置记录
         */
        //BOM内外饰配置
        List<VehicleConfigInOutColorQueryRespVO> vehicleConfigInOutColorForApp = new ArrayList<>();
        String[] inColors = carSellPartnerVO.getColorIdIns().split(",");
        for(String item : inColors){
            //BOM内饰配置
            VehicleConfigInOutColorQueryReqVO vehicleConfigInOutColorQueryReqVO = new VehicleConfigInOutColorQueryReqVO();
            vehicleConfigInOutColorQueryReqVO.setNodeCode(carSellPartnerVO.getCatalogCode());
            vehicleConfigInOutColorQueryReqVO.setColor(item);

            List<VehicleConfigInOutColorQueryRespVO> vehicleConfigInOutColorForApp_Tmp = vehicleConfigDubboService.queryVehicleConfigInColorByApp(vehicleConfigInOutColorQueryReqVO);
            vehicleConfigInOutColorForApp.addAll(vehicleConfigInOutColorForApp_Tmp);
        }

        String[] outColors = carSellPartnerVO.getColorIdOuts().split(",");
        for(String item : outColors){
            //BOM外饰配置
            VehicleConfigInOutColorQueryReqVO vehicleConfigInOutColorQueryReqVO = new VehicleConfigInOutColorQueryReqVO();
            vehicleConfigInOutColorQueryReqVO.setNodeCode(carSellPartnerVO.getCatalogCode());
            vehicleConfigInOutColorQueryReqVO.setColor(item);

            List<VehicleConfigInOutColorQueryRespVO> vehicleConfigInOutColorForApp_Tmp = vehicleConfigDubboService.queryVehicleConfigOutColorByApp(vehicleConfigInOutColorQueryReqVO);
            vehicleConfigInOutColorForApp.addAll(vehicleConfigInOutColorForApp_Tmp);
        }

        List<VehiclePackageConfigVO> vehiclePackageConfigVO = new ArrayList<>();

        for(VehicleConfigInOutColorQueryRespVO vo : vehicleConfigInOutColorForApp){

            VehiclePackageConfigVO entity = new VehiclePackageConfigVO();
            entity.setEffectiveFrom(WgrDate.date2Str(vo.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
            entity.setFamilyCode(vo.getFamilyCode());
            entity.setFamilyName(vo.getFamilyName());
            entity.setFeatureCode(vo.getFeatureCode());
            entity.setFeatureName(vo.getFeatureName());
            entity.setFeatureValueStatus(vo.getFeatureValueStatus());
            entity.setProductNodeCode(vo.getParentNodeCode());
            entity.setOptionType(carSellPartnerVO.getOptionType());
            entity.setStatus("1");
            entity.setVehicleCode(vo.getVehicleCode());
            entity.setNodeNme(vo.getNodeName());
            vehiclePackageConfigVO.add(entity);
        }

        if(vehiclePackageConfigVO.size() <= 0){
            logger.info("【BOM内外饰配置】查询没有有效数据，无法推送APP，请核实！");
            retMsg.add("【BOM内外饰配置】查询没有有效数据，无法推送APP，请核实！");
        }

        //发送BOM选装包数据给APP
        List<VehiclePackageForAppRespVO> vehiclePackageForAppList = new ArrayList<>();

        //得到已配置的选装包分类
        String[] customPacks = carSellPartnerVO.getCustomPacks().split(",");
        for(String item : customPacks){
            if(StringUtils.isNotEmpty(item)){
                VehiclePackageForAppReqVO vehiclePackageForAppReqVO = new VehiclePackageForAppReqVO();
                vehiclePackageForAppReqVO.setNodeCode(carSellPartnerVO.getCatalogCode());
                vehiclePackageForAppReqVO.setCustomPack(item);
                //根据车型及选装包分类 查询 选装包数据
                List<VehiclePackageForAppRespVO> vehiclePackageForApp_Tmp = vehiclePackageDubboService.queryVehiclePackageByApp(vehiclePackageForAppReqVO);
                if(vehiclePackageForApp_Tmp.size()>0){
                    //如果查询的价格为空 则查询最新的
                    VehiclePackageForAppRespVO pack = vehiclePackageForApp_Tmp.get(0);
                    vehiclePackageForAppList.addAll(vehiclePackageForApp_Tmp);
                }else{
                    retMsg.add("该"+carSellPartnerVO.getCatalogCode()+"|"+item+"选装包没有信息，不能放行");
                }
            }
        }

        List<VehiclePackagePriceVO> sendVehiclePackagePrice = new ArrayList<>();
        //发送选装包价格数据到APP
        for(VehiclePackageForAppRespVO vo : vehiclePackageForAppList){
            VehiclePackagePriceVO result = new VehiclePackagePriceVO();
            result.setFamilyCode(vo.getFamilyCode());
            result.setFamilyName(vo.getFamilyName());
            result.setFamilyNameEn(vo.getFamilyName());
            result.setFeatureCode(vo.getFeatureCode());
            result.setFeatureContent(vo.getFeatureContent());
            result.setFeatureDesc(vo.getFeatureDesc());
            result.setFeatureDescEn(vo.getFeatureDescEn());
            result.setFeatureEngName("");
            result.setFeatureName(vo.getFeatureName());
            result.setOptionType(carSellPartnerVO.getOptionType());
            result.setVehicleStatus(vo.getStatus() != null ? vo.getStatus().toString() : "1" );
            result.setPackageRetailPrice(vo.getAmount() != null ?vo.getAmount().toString():"0");
            result.setFeatureValueStatus(vo.getFeatureValueStatus());
            result.setVehicleCode(vo.getProductCode());
            sendVehiclePackagePrice.add(result);
        }

        if(retMsg.size() > 0){
            throw new RuntimeException(JSONArray.toJSONString(retMsg));
        }else{
            CarSellPartner carSellPartner = new CarSellPartner();
            String[] src = carSellPartnerVO.getCustomPacks().split(",");
            SortUtil.sort(src);
            BeanUtils.copyProperties(carSellPartnerVO,carSellPartner);
            carSellPartner.setCustomPacks(String.join(",",src));
            //carSellPartner.setAreas(vehicleRetailPriceVO.getExt1());
            //carSellPartner.setPartnerCodes(vehicleRetailPriceVO.getPartnerCode());
            carSellPartnerService.saveOrUpdate(carSellPartner);

            ApiResult apiResult1 = appDubboServiceImpl.sendVehicleRetailPricePMS2APP(vehicleRetailPriceVO);
            ApiResult apiResult2 = appDubboServiceImpl.sendVehiclePackagePMS2APP(vehiclePackageConfigVO);
            ApiResult apiResult3 = appDubboServiceImpl.sendVehiclePackagePricePMS2APP(sendVehiclePackagePrice);
            if(apiResult1.getCode()==200&&apiResult2.getCode()==200&&apiResult3.getCode()==200){
                return ResponseUtil.ok();
            }else if(apiResult1.getCode()!=200){
                return apiResult1;
            }else if(apiResult2.getCode()!=200){
                return apiResult2;
            }else if(apiResult3.getCode()!=200){
                return apiResult3;
            }else{
                return ResponseUtil.ok();
            }
        }
    }

    @Override
    public void removeById(String id) {
        carSellPartnerService.removeById(id);
    }

    @Override
    public PageInfo<CarSellPartnerQueryRespVO> findPageByVO(CarSellPartnerQueryReqVO carSellPartnerVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
      PageInfo<CarSellPartnerQueryRespVO> pageInfo = carSellPartnerService.pageByVo(carSellPartnerVO,pageNum,pageSize);
      return pageInfo;
   }

    @Override
    public void removeByListVo(List<CarSellPartnerQueryReqVO> carSellPartnerVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        carSellPartnerService.removeByListVo(carSellPartnerVOList);
    }

    @Override
    public List listByVo(CarSellPartnerQueryReqVO carSellPartnerVO) {
        return carSellPartnerService.listByVo(carSellPartnerVO);
    }

    @Override
    public boolean setAppStatus(CarSellPartnerSetAppStatusReqVO vo) {

        if(vo == null || vo.getSellIds() == null){
            throw new RuntimeException("vo对象不能为Null！");
        }

        for(String id : vo.getSellIds()){
            QueryWrapper qwUpdate = new QueryWrapper();
            qwUpdate.eq("SELL_ID",id);
            CarSellPartner carSellPartner = new CarSellPartner();
            carSellPartner.setAppStatus(vo.getAppStatus());
            carSellPartnerService.update(carSellPartner,qwUpdate);
        }
        return true;
    }

    /**
     * 验证选装包+配置是否已经存在
     * @return
     */
    public boolean verifyExists(CarSellPartnerVerifyExistsReqVO vo){
         QueryWrapper qw = new QueryWrapper();

         String[] src = vo.getCustomPacks().split(",");
         SortUtil.sort(src);
         vo.setCustomPacks(String.join(",",src));
         qw.eq("CATALOG_CODE",vo.getCatalogCode());
        // qw.eq("CUSTOM_PACKS",vo.getCustomPacks());

         CarSellPartner carSellPartner = carSellPartnerService.getOne(qw);

         if(carSellPartner != null){
             return true;
         }else{
             return false;
         }
    }
}
